package com.yl.mine.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.yl.common.bean.DailyModel
import com.yl.common.bean.b
import com.yl.common.util.AppGlobal
import com.yl.common.util.Constants
import com.yl.common.util.DateUtils
import com.yl.mine.R

//import com.youth.banner.BannerConfig
//import com.youth.banner.Transformer
//import com.youth.banner.loader.ImageLoader


class MineAdapter(var listener: MineItemClickListener) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>(), View.OnClickListener {

    private var list_path: ArrayList<String>? = null
    private var list_title: ArrayList<String>? = null

    private var bannerList: AsyncListDiffer<b>
    private var diff: AsyncListDiffer<b>
    private val NORMAL: Int = 0
    private val FOOT: Int = 1
    private val LAST: Int = 2
    var isLastPage = false

    init {
        bannerList = AsyncListDiffer(this, MyCallback())
        diff = AsyncListDiffer(this, MyCallback())
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            NORMAL -> {
                ArticleViewHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_mine, parent, false)
                )
            }
            FOOT -> {
                ArticleFootHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.foot_rv, parent, false)
                )
            }
            else -> {
                ArticleLastHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.last_rv, parent, false)
                )
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        Log.d("HomeRVAdapter", "onBindViewHolder: position = $position")
        if (getItemViewType(position) == NORMAL) {
            val articleViewHolder = holder as ArticleViewHolder;
            val data = diff.currentList[position]
            if (data.type == "textHeader") {
                articleViewHolder.text_header.visibility = View.VISIBLE
//                articleViewHolder.banner.visibility = View.VISIBLE
                articleViewHolder.ll_user.visibility = View.GONE
                articleViewHolder.line.visibility = View.GONE
                articleViewHolder.fl.visibility = View.GONE
                articleViewHolder.text_header.text = data.data.text
//                articleViewHolder.banner.setBannerTitles(list_title)
//                articleViewHolder.banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE)
//                articleViewHolder.banner.setImageLoader(MyLoader())
//                articleViewHolder.banner.setImages(list_path);
//                articleViewHolder.banner.setBannerAnimation(Transformer.Default)
//                articleViewHolder.banner.setDelayTime(3000)
//                articleViewHolder.banner.isAutoPlay(true)
//                articleViewHolder.banner.setIndicatorGravity(BannerConfig.RIGHT)
//                articleViewHolder.banner.setOnBannerListener {
//                    Log.e("tag", "你点了第" + position + "张轮播图")
////                    val intent = Intent(AppGlobal.get(), PlayVideoActivity::class.java)
////                    val bundle = Bundle()
////                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK )
////                    bundle.putString("data", Gson().toJson(data))
////                    intent.putExtra("Message", bundle)
////                    AppGlobal.get()!!.startActivity(intent)
//                }
//                articleViewHolder.banner.start()
            } else {
//                articleViewHolder.banner.visibility = View.GONE
                articleViewHolder.text_header.visibility = View.GONE
                articleViewHolder.fl.visibility = View.VISIBLE
                articleViewHolder.line.visibility = View.VISIBLE
                articleViewHolder.ll_user.visibility = View.VISIBLE
                articleViewHolder.type.text = data.data.category
                articleViewHolder.item_title.text = data.data.title
                articleViewHolder.user_name.text = data.data.author!!.name
                articleViewHolder.time.text =
                    DateUtils.formatDateMsByMS(data.data.duration * 1000).toString()

                Glide.with(AppGlobal.get()!!).load(data.data.cover!!.homepage).apply(
                    RequestOptions()
                        .transforms(
                            CenterCrop(), RoundedCorners(10)
                        )
                )
                    .into(articleViewHolder.image)

                Glide.with(AppGlobal.get()!!).load(data.data.author!!.icon).apply(
                    RequestOptions()
                        .transforms(
                            CenterCrop(), RoundedCorners(20)
                        )
                ).into(articleViewHolder.user_img)
            }
            articleViewHolder.itemView.tag = position
            articleViewHolder.itemView.setOnClickListener(this)

            holder.ll_item.tag = position
            holder.ll_item.setOnClickListener(this)

            holder.share_img.tag = position
            holder.share_img.setOnClickListener(this)

        }
    }

    override fun getItemCount(): Int {
        return if (diff.currentList.size == 0) 1 else diff.currentList.size + 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            if (isLastPage) {
                LAST
            } else {
                FOOT
            }
        } else {
            NORMAL
        }
    }

    class MyCallback : DiffUtil.ItemCallback<b>() {
        override fun areItemsTheSame(
            oldItem: b,
            newItem: b
        ): Boolean {
            return oldItem.adIndex == newItem.adIndex
        }

        override fun areContentsTheSame(
            oldItem: b,
            newItem: b
        ): Boolean {
            return oldItem.type == newItem.type && oldItem.type == newItem.type
        }
    }

    class ArticleViewHolder(item: View) : RecyclerView.ViewHolder(item) {
        var type: TextView = item.findViewById(R.id.type)
        var image: ImageView = item.findViewById(R.id.image)
        var ll_user: LinearLayout = item.findViewById(R.id.ll_user)
        var line: LinearLayout = item.findViewById(R.id.line)
        var ll_item: LinearLayout = item.findViewById(R.id.ll_item)
        var time: TextView = item.findViewById(R.id.time)
        var text_header: TextView = item.findViewById(R.id.text_header)

        var user_img: ImageView = item.findViewById(R.id.user_img)
        var item_title: TextView = item.findViewById(R.id.item_title)
        var user_name: TextView = item.findViewById(R.id.user_name)
        var share_img: ImageView = item.findViewById(R.id.share_img)
        var fl: FrameLayout = item.findViewById(R.id.fl)
//        var banner = item.findViewById(R.id.view_banner) as Banner

    }

    class ArticleFootHolder(item: View) : RecyclerView.ViewHolder(item)

    class ArticleLastHolder(item: View) : RecyclerView.ViewHolder(item)

    private var lastClickTime: Long = 0

    override fun onClick(v: View?) {
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime > Constants.MIN_CLICK_DELAY_TIME && v != null) {
            lastClickTime = currentTime
            Log.e("TAG", "data   ${v.tag}")
            if (v.id == R.id.share_img) {
                listener.onCollectClick(v.tag as Int)
            } else {
                listener.onItemClick(v.tag as Int)
            }
        }
    }


    fun setBannerData(list: ArrayList<DailyModel.Issue.Item>) {
        bannerList.submitList(if (list != null) ArrayList(list) else null)
        list_path = ArrayList()
        list_title = ArrayList()

        for (i in 0..bannerList.currentList.size - 1) {
            list_path!!.add(bannerList.currentList[i].data.cover!!.detail)
            list_title!!.add(bannerList.currentList[i].data.title)
        }
    }

    fun setData(list: List<b>?) {
        diff.submitList(if (list != null) ArrayList(list) else null)
    }
}

//private class MyLoader : ImageLoader() {
//    override fun displayImage(context: Context, path: Any, imageView: ImageView) {
//        Glide.with(context).load(path).into(imageView)
//    }
//}